package uimessages

import (
	"github.com/sasha-s/go-deadlock"
	"time"
)

var msgIdlock = &deadlock.RWMutex{}
var totalMsgId uint32 = 0

type UIMessage struct {
	MsgId        uint32
	Level        int
	Code         string
	Msg          string
	Parameter    interface{}
	MsgType      int
	Status       int
	Vid          int
	CreatedTime  time.Time
	ResolvedTime time.Time
	MainTaskNo   string
	SubTaskNo    string
}

func NewUIMessage(level int, code string, msg string, parameter interface{}, msgType int, status int, vid int, mainTaskNo string, subTaskNo string) *UIMessage {
	return &UIMessage{
		MsgId:       generateMsgId(),
		Level:       level,
		Code:        code,
		Msg:         msg,
		Parameter:   parameter,
		MsgType:     msgType,
		Status:      status,
		Vid:         vid,
		CreatedTime: time.Now(),
		MainTaskNo:  mainTaskNo,
		SubTaskNo:   subTaskNo,
	}
}

func generateMsgId() uint32 {
	var retMsgId uint32 = 0

	msgIdlock.Lock()
	totalMsgId++
	retMsgId = totalMsgId
	msgIdlock.Unlock()
	return retMsgId
}
